use "AmericasBarometer Grand Merge 2004-2014 v3.0_FREE.dta", clear

drop if pais == 11 & year == 2010												// the Peru data for the grandmerge file is missing key variables 

**** The following country years are missing from the "Grand Merge" file (version: 31 Oct 2015). To reconstruct the LAPOP 
**** dataset used in Study 1, you have to download the separate 
**** country files and append those observations for the key variables for the analysis:
**** Peru 2010, Ecuador 2014, Bolivia 2010, Bolivia 2012, Bolivia 2014, Chile 2010, Chile 2012, Chile 2014, 
**** Venezuela 2010, Venezuela 2012, Venezuela 2014, Argentina 2010, Argentina 2012, Argentina 2014

keep if year >= 2010															// No skin color data before 2010

drop if pais > 21																// with caribbean non-LA countries dropped from the model 

*******************************************
********* Survey Design *******************
*******************************************

replace wt = 1 if (pais == 16 & year == 2010) | (pais == 17 & year == 2010) | (pais == 11 & year == 2010)

svyset upm [pweight=wt], strata(estratopri)  

*******************************************
********* Geo Controls ********************
*******************************************

gen urban = ur 
	replace urban = 0 if urban == 2
	
tab tamano, gen(tamano_)
	rename tamano_1 capital
	rename tamano_2 largecity
	rename tamano_3 mediumcity
	rename tamano_4 smallcity
	rename tamano_5 ruralarea
	
*******************************************
********* Race and Skin Color *************
*******************************************

gen afro_id = .
	replace afro_id = 1 if etid == 4 | etid == 5
	replace afro_id = 1 if etid == 2106 & pais == 21
	replace afro_id = 1 if etid == 1113 & pais == 11
	replace afro_id = 1 if etid == 6 & pais == 16
	replace afro_id = 0 if afro_id == . 
		
gen ind_id = . 
	replace ind_id = 1 if etid == 3
	replace ind_id = 1 if etid == 1110 & pais == 11
	replace ind_id = 1 if etid == 1111 & pais == 11
	replace ind_id = 1 if etid == 1112 & pais == 11
	replace ind_id = 0 if ind_id == . 
	
tab etid, gen(race_)
	rename race_1 white 
	rename race_2 mestizo 
	rename race_3 indigenous 
		replace indigenous = 1 if race_9==1 | race_10 == 1 | race_11 == 1 | race_12 == 1
	rename race_4 black 
		replace black = 1 if race_5== 1
		replace black = 1 if race_6== 1  
		replace black = 1 if race_14 == 1 | race_15 == 1
	rename race_7 other  
		replace other = 1 if race_8 == 1 | race_13 == 1
		
gen etid_new = . 
	replace etid_new = 1 if white == 1 
	replace etid_new = 2 if mestizo == 1 
	replace etid_new = 3 if indigenous == 1 
	replace etid_new = 4 if black == 1 
	replace etid_new = 5 if other == 1 
		
gen afro_indigenous = . 
	replace afro_indigenous = 1 if ind_id == 1
	replace afro_indigenous = 0 if afro_id == 1
	
gen afro_indigenous2 = . 
	replace afro_indigenous2 = 1 if ind_id == 1 | afro_id == 1 
	replace afro_indigenous2 = 0 if ind_id == 0 & afro_id == 0
	
drop if colorr == 97 | colorr == .a | colorr == .b | colorr == .c | colorr == .d	 // getting rid of observations that lack the main skin color measure 
	
tab estratopri, gen(region_)

gen colorr_sd_region=.			// standardized color variable by REGION ; missing skin color data: region_30 region_31 region_32 - three regions in nicaragua; region_54 region_55 region_56 region_57 region_58 region_59 region_60 region_61 region_62 region_63 - bolivian regions; region_76 region_77 region_78 region_79 region_80 region_81 region_82 region_83 region_84 - chilean regions; region_92 region_93 region_94 region_95 region_96 region_97 - Venezuela; region_98 region_99 region_100 region_101 region_102 region_103 
foreach i in region_1 region_2 region_3 region_4 region_5 region_6 region_7 region_8 region_9 region_10 region_11 region_12 region_13 region_14 region_15 region_16 region_17 region_18 region_19 region_20 region_21 region_22 region_23 region_24 region_25 region_26 region_27 region_28 region_29 region_30 region_31 region_32 region_33 region_34 region_35 region_36 region_37 region_38 region_39 region_40 region_41 region_42 region_43 region_44 region_45 region_46 region_47 region_48 region_49 region_50 region_51 region_52 region_53 region_54 region_55 region_56 region_57 region_58 region_59 region_60 region_61 region_62 region_63 region_64 region_65 region_66 region_67 region_68 region_69 region_70 region_71 region_72 region_73 region_74 region_75 region_76 region_77 region_78 region_79 region_80 region_81 region_82 region_83 region_84 region_85 region_86 region_87 region_88 region_89 region_90 region_91 region_92 region_93 region_94 region_95 region_96 region_97 region_98 region_99 region_100 region_101 region_102 region_103 region_104 region_105 region_106 region_107 region_108 {
svy, subpop(`i'): mean colorr  
estat sd
mat m = r(mean)
mat sd = r(sd)
replace colorr_sd_region= (colorr - `=m[1,1]') / `=sd[1,1]'
}

drop region_*

/*gen color_category_region=.     
	replace color_category_region = 1 if colorr_sd_region <= -2 
	replace color_category_region = 2 if colorr_sd_region > -2 & colorr_sd_region <= -0.75
	replace color_category_region = 3 if colorr_sd_region > -0.75 & colorr_sd_region <= .25
	replace color_category_region = 4 if colorr_sd_region > .25 & colorr_sd_region <= 1.75
	replace color_category_region=5 if colorr_sd_region > 1.75*/
	
gen color_category_region2 = . 
	replace color_category_region2 = 1 if colorr_sd_region <= -2 
	replace color_category_region2 = 2 if colorr_sd_region > -2 & colorr_sd_region <= -1
	replace color_category_region2 = 3 if colorr_sd_region > -1 & colorr_sd_region <= 0
	replace color_category_region2 = 4 if colorr_sd_region > 0 & colorr_sd_region <= 1
	replace color_category_region2 = 5 if colorr_sd_region > 1 & colorr_sd_region <= 2
	replace color_category_region2 = 6 if colorr_sd_region > 2 

gen color_category_region3=.     
	replace color_category_region3=1 if colorr_sd_region > -2.5 & colorr_sd_region <= -1.5
	replace color_category_region3=2 if colorr_sd_region > -1.5 & colorr_sd_region <= -0.5
	replace color_category_region3=3 if colorr_sd_region > -0.5 & colorr_sd_region < 0.5
	replace color_category_region3=4 if colorr_sd_region >= 0.5 & colorr_sd_region < 1.5
	replace color_category_region3=5 if colorr_sd_region >= 1.5 & colorr_sd_region < 2.5
	replace color_category_region3=6 if colorr_sd_region >= 2.5 & colorr_sd_region < 3.5

gen color_category_region4 = . 
	replace color_category_region4 = 1 if color_category_region2 == 1 | color_category_region2 == 2 
	replace color_category_region4 = 2 if color_category_region2 == 3
	replace color_category_region4 = 3 if color_category_region2 == 4 
	replace color_category_region4 = 4 if color_category_region2 == 5 
	replace color_category_region4 = 5 if color_category_region2 == 6 
			
tab pais, gen(country_)															// alternative coding of skin color, standardized at the country level 

gen colorr_sd_country=.			
foreach i in country_1 country_2 country_3 country_4 country_5 country_6 country_7 country_8 country_9 country_10 country_11 country_12 country_13 country_14 country_15 country_16 country_17 country_18 {
svy, subpop(`i'): mean colorr  
estat sd
mat m = r(mean)
mat sd = r(sd)
replace colorr_sd_country= (colorr - `=m[1,1]') / `=sd[1,1]'
}

gen color_category_country = . 
	replace color_category_country = 1 if colorr_sd_country <= -2 
	replace color_category_country = 2 if colorr_sd_country > -2 & colorr_sd_country <= -1
	replace color_category_country = 3 if colorr_sd_country > -1 & colorr_sd_country <= 0
	replace color_category_country = 4 if colorr_sd_country > 0 & colorr_sd_country <= 1
	replace color_category_country = 5 if colorr_sd_country > 1 & colorr_sd_country <= 2
	replace color_category_country = 6 if colorr_sd_country > 2 

tab color_category_country, gen(color_)
	rename color_1 verylight_c
	rename color_2 light_c
	rename color_3 med_light_c
	rename color_4 med_dark_c 
	rename color_5 dark_c
	rename color_6 verydark_c
	
gen color_category_region = . 
	replace color_category_region = 1 if color_category_region2 == 1
	replace color_category_region = 2 if color_category_region2 == 2
	replace color_category_region = 3 if color_category_region2 == 3
	replace color_category_region = 4 if color_category_region2 == 4
	replace color_category_region = 5 if color_category_region2 == 5
	replace color_category_region = 6 if color_category_region2 == 6
	
	replace color_category_region = 1 if color_category_region3 == 1 & pais == 7 
	replace color_category_region = 2 if color_category_region3 == 2 & pais == 7 
	replace color_category_region = 3 if color_category_region3 == 3 & pais == 7 
	replace color_category_region = 4 if color_category_region3 == 4 & pais == 7 
	replace color_category_region = 5 if color_category_region3 == 5 & pais == 7  
	replace color_category_region = 6 if color_category_region3 == 6 & pais == 7 
	
	replace color_category_region = 5 if color_category_region == 6 & (pais == 11 | pais == 10 | pais == 6 | pais == 2 | pais == 1) // cases that drop very dark (level 6)

	tab color_category_region, gen(color_)
		rename color_1 verylight
		rename color_2 light
		rename color_3 med_light
		rename color_4 med_dark
		rename color_5 dark
		rename color_6 verydark

gen vl_vd = . 
	replace vl_vd = 0 if verylight == 1
	replace vl_vd = 1 if verydark == 1
	
gen dark_i = . 
	replace dark_i = 1 if med_dark == 1 | dark == 1 | verydark == 1
	replace dark_i = 0 if med_dark == 0 & dark == 0 & verydark == 0

	
******************************************
******** Wealth and Income ***************
******************************************

** income 
replace q10 = q10new if pais == 9 & year == 2014 
replace q10 = q10new if pais == 10 & year > 2010 
replace q10 = q10new if pais == 13 & year > 2010 
replace q10 = q10new if pais == 16 & year > 2010 
replace q10 = q10new if pais == 17 & year > 2010 
replace q10 = q10new if pais == 1 & year > 2010
replace q10 = q10new if pais == 2 & year > 2010
replace q10 = q10new if pais == 3 & year > 2010
replace q10 = q10new if pais == 4 & year > 2010
replace q10 = q10new if pais == 5 & year > 2010
replace q10 = q10new if pais == 6 & year > 2010
replace q10 = q10new if pais == 7 & year > 2010
replace q10 = q10new if pais == 8 & year > 2010
replace q10 = q10new if pais == 9 & year == 2012
replace q10 = q10new if pais == 11 & year > 2010
replace q10 = q10new if pais == 12 & year > 2010
replace q10 = q10new if pais == 14 & year > 2010
replace q10 = q10new if pais == 15 & year > 2010
replace q10 = q10new if pais == 21 & year > 2010

** wealth 
	
rename r1 tv
rename r3 fridge
rename r4 phone
rename r4a cell
rename r5 cars
rename r6 washer
rename r7 microwave
rename r8 moto
rename r12 potable
rename r14 bathroom
rename r15 computer
rename r18 internet
rename r16 flatscreen
rename r26 sewage

gen onecar=.
	replace onecar=1 if cars==1
	replace onecar=0 if cars !=1
	
gen nocar=.
	replace nocar=1 if cars==0
	replace nocar=0 if cars !=0
	
gen twocar=.
	replace twocar=1 if cars==2
	replace twocar=0 if cars !=2
	
gen threecar=.
	replace threecar=1 if cars==3
	replace threecar=0 if cars !=3

gen firstq = . 
gen secondq = . 
gen thirdq = . 
gen fourthq = . 
gen fifthq = .

foreach var in country_1 country_2 country_3 country_4 country_5 country_6 country_7 ///
country_8 country_9 country_10 country_11 country_12 country_13 country_14 country_15 ///
country_16 country_17 country_18 {												// wealth all 
  pca tv fridge phone cell nocar onecar twocar threecar washer microwave moto potable bathroom computer if year == 2010 & `var' == 1
  predict wealth10 if year == 2010 & `var' == 1, score
  centile wealth10 if `var' == 1 & year == 2010, c(0, 20)
	replace firstq = (wealth10 >= r(c_1) & wealth10 < r(c_2)) if wealth10 != . & `var'== 1 & year == 2010
  centile wealth10 if `var' == 1 & year == 2010, c(20, 40)
	replace secondq = (wealth10 >= r(c_1) & wealth10 < r(c_2)) if wealth10 != . & `var'== 1 & year == 2010
  centile wealth10 if `var' == 1 & year == 2010, c(40, 60)
	replace thirdq = (wealth10 >= r(c_1) & wealth10 < r(c_2)) if wealth10 != . & `var'== 1 & year == 2010
  centile wealth10 if `var' == 1 & year == 2010, c(60, 80)
	replace fourthq = (wealth10 >= r(c_1) & wealth10 <= r(c_2)) if wealth10 != . & `var'== 1 & year == 2010
  centile wealth10 if `var' == 1 & year == 2010, c(80, 100)
	replace fifthq = (wealth10 >= r(c_1) & wealth10 <= r(c_2)) if wealth10 != . & `var'== 1 & year == 2010
  drop wealth10
	
  pca tv fridge phone cell nocar onecar twocar threecar washer microwave moto potable bathroom computer if year == 2012 & `var' == 1
  predict wealth12 if year == 2012 & `var' == 1, score
  centile wealth12 if `var' == 1 & year == 2012, c(0, 20)
	replace firstq = (wealth12 >= r(c_1) & wealth12 < r(c_2)) if wealth12 != . & `var'== 1 & year == 2012
  centile wealth12 if `var' == 1 & year == 2012, c(20, 40)
	replace secondq = (wealth12 >= r(c_1) & wealth12 < r(c_2)) if wealth12 != . & `var'== 1 & year == 2012
  centile wealth12 if `var' == 1 & year == 2012, c(40, 60)
	replace thirdq = (wealth12 >= r(c_1) & wealth12 < r(c_2)) if wealth12 != . & `var'== 1 & year == 2012
  centile wealth12 if `var' == 1 & year == 2012, c(60, 80) 
	replace fourthq = (wealth12 >= r(c_1) & wealth12 <= r(c_2)) if wealth12 != . & `var'== 1 & year == 2012
  centile wealth12 if `var' == 1 & year == 2012, c(80, 100)
	replace fifthq = (wealth12 >= r(c_1) & wealth12 <= r(c_2)) if wealth12 != . & `var'== 1 & year == 2012
  drop wealth12
  
  pca tv fridge phone cell nocar onecar twocar threecar washer microwave moto potable bathroom computer if year == 2014 & `var' == 1
  predict wealth14 if year == 2014 & `var' == 1, score
  centile wealth14 if `var' == 1 & year == 2014, c(0, 20)
	replace firstq = (wealth14 >= r(c_1) & wealth14 < r(c_2)) if wealth14 != . & `var'== 1 & year == 2014
  centile wealth14 if `var' == 1 & year == 2014, c(20, 40)
	replace secondq = (wealth14 >= r(c_1) & wealth14 < r(c_2)) if wealth14 != . & `var'== 1 & year == 2014
  centile wealth14 if `var' == 1 & year == 2014, c(40, 60)
	replace thirdq = (wealth14 >= r(c_1) & wealth14 < r(c_2)) if wealth14 != . & `var'== 1 & year == 2014
  centile wealth14 if `var' == 1 & year == 2014, c(60, 80)
	replace fourthq = (wealth14 >= r(c_1) & wealth14 <= r(c_2)) if wealth14 != . & `var'== 1 & year == 2014
  centile wealth14 if `var' == 1 & year == 2014, c(80, 100)
	replace fifthq = (wealth14 >= r(c_1) & wealth14 <= r(c_2)) if wealth14 != . & `var'== 1 & year == 2014
  drop wealth14
}

gen wealth_quintile=.
	replace wealth_quintile=1 if firstq==1
	replace wealth_quintile=2 if secondq==1
	replace wealth_quintile=3 if thirdq==1
	replace wealth_quintile=4 if fourthq==1
	replace wealth_quintile=5 if fifthq==1
	
gen wealth_quintile_richtopoor = ((wealth_quintile - 5)* -1) + 1				

******************************************
******** Target of Clientelism ***********
******************************************

gen votebuy = .
	replace votebuy= 1 if clien1 < 3
	replace votebuy= 0 if clien1 == 3
	replace votebuy= 1 if clien1na == 1
	replace votebuy= 0 if clien1na == 2
	
gen votebuy_times = . 
	replace votebuy_times = 0 if clien1 == 3
	replace votebuy_times = 1 if clien1 == 2
	replace votebuy_times = 2 if clien1 == 1
	
gen votebuy_other = . 
	replace votebuy_other = 0 if clien1n == 2
	replace votebuy_other = 1 if clien1n == 1

******************************************
************ Partisanship ****************
******************************************

gen partisan=(vb10-2)*-1

gen pol_meeting = . 
	replace pol_meeting = 1 if cp13 == 1 | cp13 == 2 | cp13 == 3
	replace pol_meeting = 0 if cp13 == 4
	
gen campaigner = . 
	replace campaigner = 1 if pp2 == 1
	replace campaigner = 0 if pp2 == 2
	
	
gen partisan_index = campaigner + pol_meeting									// Holland and Palmer-Rubin; no data for 2014

******************************************
*************** Requester ****************										// Nichter and Peress 
******************************************
 
gen requester_local = (cp4a-2)*-1

******************************************
*********** Persuasion Frequency *********										// Schaffer and Baker  
******************************************

gen persuasion_freq = (pp1 - 4) * -1 

******************************************
**************** Civic *******************										// Holland and Palmer-Rubin  
******************************************

gen problem = . 
	replace problem = 1 if cp5 < 4
	replace problem = 0 if cp5 == 4
gen religious = . 
	replace religious = 1 if cp6 < 4
	replace religious = 0 if cp6 == 4
gen school = . 
	replace school = 1 if cp7 < 4
	replace school = 0 if cp7 == 4 
gen improvement = . 
	replace improvement = 1 if cp8 < 4
	replace improvement = 0 if cp8 == 4
gen professional = . 															// lots of missing data for 2014
	replace professional = 1 if cp9 <4	
	replace professional = 0 if cp9 == 4
gen mothers = . 																//lots of missing data across years 
	replace mothers = 1 if cp20 <4 
	replace mothers = 0 if cp20 == 4
gen political_meet = . 
	replace political_meet = 1 if cp13 <4
	replace political_meet = 0 if cp13 ==4
	
gen civic = school + problem + religious + improvement + professional + mothers 

gen civic2 = school + problem + religious + improvement
	replace civic2 = school + problem + religious if improvement == . 
	replace civic2 = school + problem + improvement if religious == . 
	replace civic2 = school + religious + improvement if problem == . 
	replace civic2 = problem + religious + improvement if school == . 
	replace civic2 = school + problem if religious == . & improvement == . 
	replace civic2 = school + religious if problem == . & improvement == . 
	replace civic2 = school + improvement if problem == . & religious  == . 
	replace civic2 = problem + religious if school == . & improvement  == . 
	replace civic2 = problem + improvement if school == . & religious  == . 
	replace civic2 = religious + improvement if school == . & problem  == . 
	replace civic2 = religious if school == . & problem  == . & improvement == . 
	replace civic2 = school if religious == . & problem  == . & improvement == . 
	replace civic2 = problem if religious == . & school  == . & improvement == . 
	replace civic2 = improvement if religious == . & school  == . & problem == . 

******************************************
**** Voted and Participation Index *******										 
******************************************

gen voted = .
	replace voted = 1 if vb2 == 1
	replace voted = 0 if vb2 == 2
	replace voted = 1 if pervb2b == 1
	replace voted = 0 if pervb2b == 2

gen protest = (prot3 - 2)* -1 
gen attend_council = (np1 - 2) * -1
gen would_vote = . 
	replace would_vote = 1 if vb20 > 1 
	replace would_vote = 0 if vb20 == 1
	
gen participation_index = protest + attend_council + would_vote 				// derived from Schaffer and Baker.  
	replace participation_index = protest + attend_council if would_vote == . 
	replace participation_index = protest + would_vote if attend_council == . 
	replace participation_index = attend_council + would_vote if protest == . 
	replace participation_index = attend_council if would_vote == . & protest == . 
	replace participation_index = would_vote if attend_council == . & protest == . 
	replace participation_index = protest if attend_council == . & would_vote == . 
	
gen pol_interest = (pol1 - 4) * -1 

******************************************										// Holland and Palmer-Rubin 
******* Reciprocity Proxies **************										 
******************************************

gen i_trust = (it1 - 4) * -1

gen dependable = .
	replace dependable = 1 if per3 >= 5
	replace dependable = 0 if per3 < 5
	
gen generous = . 
	replace generous = 1 if per7 >= 5
	replace generous = 0 if per7 < 5
	
gen neglectful_rev = . 
	replace neglectful_rev = 1 if per8 <= 3
	replace neglectful_rev = 0 if per8 > 3
	
gen reliability = dependable + generous + neglectful_rev
	replace reliability = . if year > 2010

******************************************
************* Registered *****************										 
******************************************

gen registered = (vb1 - 2) * - 1												 
	replace registered = 1 if registered == - 1	

******************************************
********* Trust in Elections *************										 
******************************************

gen b47_trusttonotrust = ((b47 - 7) * -1) + 1	

******************************************
*********** Age and Gender****************										 
******************************************

rename q1 female
	replace female= female-1

rename q2 age

******************************************
******** Discrimination Measures *********										 
******************************************

gen govdis = .																	// perceive discrim in government offices 
	replace govdis = (dis2-2)*-1

gen schooldis = . 
	replace schooldis = (dis3-2)*-1
	
gen publicdis = .																
	replace publicdis = (dis5-2)*-1
 
gen colordis = .
	replace colordis = (dis11 - 4) * -1

gen speechdis = .
	replace speechdis = (dis17 - 4) * -1 
	
gen classdis = .
	replace classdis = (dis13 - 4) * -1 

gen genderdis = .
	replace genderdis = (dis12 - 4) * -1 
	
******************************************
************ Bribery Measures ************										 
******************************************

gen bribery_1 = . 
	replace bribery_1 = 1 if exc2 == 1 | exc6 == 1 | exc11 == 1 | exc13 == 1 | exc14 == 1 | exc15 == 1 | exc16 == 1 
	
gen bribery_3 = . 
	replace bribery_3 = 1 if exc2 == 1 | exc6 == 1 | exc11 == 1 | exc13 == 1 | exc14 == 1 | exc15 == 1 | exc16 == 1 
	replace bribery_3 = 0 if exc2 == 0 & exc6 == 0 & exc11 == 0 & exc13 == 0 & exc14 == 0 & exc15 == 0 & exc16 == 0 

gen bribery_2 = exc2 + exc6 + exc11 + exc13 + exc14 + exc15 + exc16
	replace bribery_2 = 0 if bribery_2 == . 
	
gen bribes_ok = exc18

gen corruption = (exc7 - 4) * -1 

******************************************
*********** Big 5 Personality ************
******************************************
gen per6_rev = ((per6 - 7) *-1) +1 
gen per10_rev = ((per10 - 7) *-1) +1 
gen per2_rev = ((per2 - 7) *-1) +1 
gen per8_rev = ((per8 - 7) *-1) +1 
gen per4_rev = ((per4 - 7) *-1) +1 

gen extraversion = (per6_rev + per1)/14
gen openness = (per10_rev + per5) / 14
gen agreeableness = (per2_rev + per7) / 14
gen conscientiousness = (per8_rev + per3) / 14
gen stability = (per4_rev + per9) / 14

 

******************************************
************ Country Rounds **************										 
******************************************

gen country_round = . 
	replace country_round = 101 if pais == 1 & year == 2010						// Mexico
	replace country_round = 102 if pais == 1 & year == 2012
	replace country_round = 103 if pais == 1 & year == 2014			
	replace country_round = 201 if pais == 2 & year == 2010						// Guatemala
	replace country_round = 202 if pais == 2 & year == 2012						
	replace country_round = 203 if pais == 2 & year == 2014						
	replace country_round = 301 if pais == 3 & year == 2010						// El Salvador						
	replace country_round = 302 if pais == 3 & year == 2012												
	replace country_round = 303 if pais == 3 & year == 2014												
	replace country_round = 401 if pais == 4 & year == 2010						// Honduras												
	replace country_round = 402 if pais == 4 & year == 2012												
	replace country_round = 403 if pais == 4 & year == 2014												
	replace country_round = 501 if pais == 5 & year == 2010						// Nicaragua 												
	replace country_round = 502 if pais == 5 & year == 2012																		
	replace country_round = 503 if pais == 5 & year == 2014																		
	replace country_round = 601 if pais == 6 & year == 2010						// Costa Rica
	replace country_round = 602 if pais == 6 & year == 2012						
	replace country_round = 603 if pais == 6 & year == 2014						
	replace country_round = 701 if pais == 7 & year == 2010						// Panama						
	replace country_round = 702 if pais == 7 & year == 2012												
	replace country_round = 703 if pais == 7 & year == 2014												
	replace country_round = 801 if pais == 8 & year == 2010						// Colombia												
	replace country_round = 802 if pais == 8 & year == 2012																		
	replace country_round = 803 if pais == 8 & year == 2014																		
	replace country_round = 901 if pais == 9 & year == 2010						// Ecuador 																	
	replace country_round = 902 if pais == 9 & year == 2012																							
	replace country_round = 903 if pais == 9 & year == 2014	
	replace country_round = 1001 if pais == 10 & year == 2010					// Bolivia
	replace country_round = 1002 if pais == 10 & year == 2012					
	replace country_round = 1003 if pais == 10 & year == 2014					
	replace country_round = 1101 if pais == 11 & year == 2010					// Peru 																							
	replace country_round = 1102 if pais == 11 & year == 2012					 																							
	replace country_round = 1103 if pais == 11 & year == 2014					 																							
	replace country_round = 1201 if pais == 12 & year == 2010					// Paraguay 					 																							
	replace country_round = 1202 if pais == 12 & year == 2012										 																							
	replace country_round = 1203 if pais == 12 & year == 2014										 																							
	replace country_round = 1301 if pais == 13 & year == 2010					// Chile										 																							
	replace country_round = 1302 if pais == 13 & year == 2012															 																							
	replace country_round = 1303 if pais == 13 & year == 2014															 																							
	replace country_round = 1401 if pais == 14 & year == 2010					// Uruguay 										 																							
	replace country_round = 1402 if pais == 14 & year == 2012																		 																							
	replace country_round = 1403 if pais == 14 & year == 2014																		 																							
	replace country_round = 1501 if pais == 15 & year == 2010					// Brazil 																		 																							
	replace country_round = 1502 if pais == 15 & year == 2012					 																		 																							
	replace country_round = 1503 if pais == 15 & year == 2014					 																		 																							
	replace country_round = 1601 if pais == 16 & year == 2010					//Venezuela
	replace country_round = 1602 if pais == 16 & year == 2012					
	replace country_round = 1603 if pais == 16 & year == 2014					
	replace country_round = 1701 if pais == 17 & year == 2010					// Argentina					
	replace country_round = 1702 if pais == 17 & year == 2012										
	replace country_round = 1703 if pais == 17 & year == 2014										
	replace country_round = 2101 if pais == 21 & year == 2010					// DR																		 																							
	replace country_round = 2102 if pais == 21 & year == 2012					 																		 																							
	replace country_round = 2103 if pais == 21 & year == 2014
	
***********************************************
*************** Country Subsets ***************
***********************************************

gen minority_country = . 
	replace minority_country = 1 if pais == 21 | pais == 6 | pais == 7 | pais ==8 | pais == 16 | pais == 15 // Costa Rica, Panama, Colombia, Brazil, Venezuela, and DR - countries with at least 10% self-id as Afro
	replace minority_country = 1 if pais == 2 | pais == 10 | pais ==1 | pais == 9 | pais == 11 // Guatemala, Bolivia, Mexico, Ecuador, Peru 
	replace minority_country = 0 if minority_country == . 

gen afro_country = .
	replace afro_country = 1 if pais == 21 | pais == 6 | pais == 7 | pais ==8 | pais == 16 | pais == 15
	replace afro_country = 0 if pais == 21 & pais == 6 & pais == 7 & pais ==8 & pais == 16 & pais == 15
	
gen ind_country = . 
	replace ind_country = 1 if pais == 2 | pais == 10 | pais ==1 | pais == 9 | pais == 11
	replace ind_country = 0 if pais == 2 & pais == 10 & pais ==1 & pais == 9 & pais == 11
	
gen ceffective_country = . 														// all are high effort systems 
	replace ceffective_country = 1 if pais == 21 | pais == 17 | pais == 8 | pais == 2 | pais == 10 | pais == 9
	replace ceffective_country = 0 if pais == 5| pais == 4 | pais == 3 | pais == 7 | pais == 1 /* | pais == 9*/
	
gen higheffort_country = . 
	replace higheffort_country = 1 if pais == 10 | pais == 1 | pais == 5 | pais == 4 | pais == 3 | pais == 7 | pais == 9 | pais == 2 | pais == 17 | pais == 8 | pais == 21 /* | pais == 15 | pais == 16 */
	replace higheffort_country = 0 if pais == 15 | pais == 16 | pais == 13 | pais == 11 | pais == 12 | pais == 6 | pais == 14

************************************************
********** Complete observations ***************
************************************************

gen complete = . 
	replace complete = 1 if color_category_region != . & wealth_quintile_richtopoor	!= . &	///
							partisan !=. & civic2 != . & pol_interest != . & participation_index != . & i_trust != . ///
							& voted != . & registered != . & ed !=. & female !=. & age !=. & urban !=. & colori != . 
	replace complete = 0 if color_category_region == . | wealth_quintile_richtopoor	== . |	///
							partisan ==. | civic2 == . | pol_interest == . | participation_index == . | i_trust == . ///
							| voted == . | registered == . | ed ==. | female ==. | age ==. | urban ==. | colori == . 
	
************************************************
****** Final variables for model and ***********
****** 		robustness checks 		 ***********
************************************************

drop if votebuy == . 
	
keep bribes_ok bribery_2 bribery_3 bribery_1 govdis schooldis publicdis colordis ///
speechdis classdis genderdis age ed female b47_trusttonotrust registered reliability ///
dependable generous neglectful_rev i_trust pol_interest participation_index voted protest ///
attend_council would_vote civic2 civic persuasion_freq requester_local partisan_index ///
campaigner pol_meeting partisan votebuy_other votebuy_times votebuy wealth_quintile_richtopoor ///
wealth_quintile q10 dark_i vl_vd color_category_country color_category_region4 color_category_region3 ///
verylight light med_light med_dark dark verydark color_category_region2 color_category_region ///
estratopri colorr colori etid_new tamano capital largecity mediumcity smallcity ruralarea urban ///
wt upm pais year country_1 country_2 country_3 country_4 country_5 country_6 country_7 country_8 ///
country_9 country_10 country_11 country_12 country_13 country_14 country_15 country_16 country_17 ///
country_18 corruption white mestizo indigenous black other country_round idnum uniq_id minority_country ///
afro_country ind_country ceffective_country higheffort_country voted extraversion openness agreeableness conscientiousness stability ///
protest attend_council would_vote vb11_10 vb11_14

melogit votebuy i.color_category_region wealth_quintile_richtopoor				///
partisan civic2 pol_interest participation_index i_trust ///
voted registered ed female age urban colori ///
if minority_country == 1 || country_round: , intpoints(10)
	
	generate sample = e(sample) 												//  For nested analysis: generating 'sample' from the full model 
	
save vbpaper_2019version.dta, replace 


/*foreach var in reliability i_trust pol_interest participation_index persuasion_freq partisan_index { // Done I decided to keep i_trust pol_interest participation_index in this version because they have data from 2010-2014
	
	display "************************** `var' *********************"
	tab `var' year 
	
} */


cap log close
log using vb_cases.log, replace

***** finding missing data culprits in the pooled model and then by country 

tab pais year if color_category_region ==. & minority_country == 1
tab pais year if female ==. & minority_country == 1
tab pais year if ed ==. & minority_country == 1
tab pais year if age ==. & minority_country == 1
tab pais year if urban ==. & minority_country == 1
tab pais year if colori == . & minority_country == 1

tab pais year if wealth_quintile_richtopoor ==. & minority_country == 1
tab pais year if partisan == . & minority_country == 1
tab pais year if requester_local ==. & minority_country == 1					// missing for Bolivia for 2010, so won't include this in the pooled or main models 
tab pais year if civic2 ==. & minority_country == 1
tab pais year if pol_interest ==. & minority_country == 1
tab pais year if participation_index ==. & minority_country == 1
tab pais year if i_trust ==. & minority_country == 1
tab pais year if voted ==. & minority_country == 1
tab pais year if registered ==. & minority_country == 1



